package com.dark;

import com.google.common.base.Function;
import com.google.common.collect.Ordering;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * Created by tengxue on 16-6-13.
 * 排序类.
 */
public class OrderingDemo {
    public static void main(String[] args){
        Ordering<Foo> ordering = Ordering.natural().nullsFirst().onResultOf(new Function<Foo, String>() {
            public String apply(Foo foo) {
                return foo.getUuid();
            }
        });

        List<Foo> arrayList=new ArrayList<>();
        Foo foo1=new Foo();
        foo1.setUuid(UUID.randomUUID().toString());
        foo1.setName("one");
        foo1.setAddress("one address");

        Foo foo2=new Foo();
        foo2.setUuid(UUID.randomUUID().toString());
        foo2.setName("two");
        foo2.setAddress("two address");

        Foo foo3=new Foo();
        foo3.setUuid(UUID.randomUUID().toString());
        foo3.setName("three");
        foo3.setAddress("three address");

        arrayList.add(foo1);
        arrayList.add(foo2);
        arrayList.add(foo3);

        System.out.println(arrayList);
        Collections.sort(arrayList, ordering);
        System.out.println(arrayList);


    }

    static class Foo{
        private String uuid;
        private String name;
        private String address;

        public String getUuid() {
            return uuid;
        }

        public void setUuid(String uuid) {
            this.uuid = uuid;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        @Override
        public String toString() {
            return "Foo{" +
                    "uuid='" + uuid + '\'' +
                    ", name='" + name + '\'' +
                    ", address='" + address + '\'' +
                    '}';
        }
    }

}
